#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from fastapi import APIRouter, Depends
from utils.response import SuccessResponse
from . import schemas, crud, params
from core.dependencies import IdList
from apps.base.auth.validate import AllUserAuth, Auth


app = APIRouter()


@app.get("/sample/group", summary="获取抽样模型组")
async def get_sample_group(
        p: params.SampleGroupParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.SampleGroupDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.SampleGroupSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/sample/group", summary="创建抽样模型组")
async def create_sample_group(
        data: schemas.SampleGroup,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    return SuccessResponse(await crud.SampleGroupDal(auth.db).create_data(data))


@app.put("/sample/group/{data_id}", summary="更新抽样模型组")
async def put_sample_group(
        data_id: int,
        data: schemas.SampleGroup,
        auth: Auth = Depends(AllUserAuth())

):
    data.update_user = auth.user.user_str
    return SuccessResponse(await crud.SampleGroupDal(auth.db).put_data(data_id, data=data))


@app.delete("/sample/group", summary="批量删除抽样模型组", description="硬删除")
async def delete_sample_group(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.SampleGroupDal(auth.db).delete_datas(ids=ids.ids)
    return SuccessResponse("删除成功")


@app.get("/sample/group/options", summary="获取抽样模型下拉选项", description="")
async def get_sample_group_options(auth: Auth = Depends(AllUserAuth())):
    datas = await crud.SampleGroupDal(auth.db).get_select_datas()
    return SuccessResponse(datas)


@app.get("/sample/standard", summary="获取抽样规范")
async def get_sample_standard(
        p: params.SampleStandardParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.SampleStandardDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.SampleStandardSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/sample/standard", summary="创建抽样规范")
async def create_sample_standard(
        data: schemas.SampleStandard,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    return SuccessResponse(await crud.SampleStandardDal(auth.db).create_data(data))


@app.put("/sample/standard/{data_id}", summary="更新抽样规范")
async def put_sample_standard(
        data_id: int,
        data: schemas.SampleStandard,
        auth: Auth = Depends(AllUserAuth())

):
    data.update_user = auth.user.user_str
    return SuccessResponse(await crud.SampleStandardDal(auth.db).put_data(data_id, data=data))


@app.delete("/sample/standard", summary="批量删除抽样规范", description="硬删除")
async def delete_sample_standard(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.SampleStandardDal(auth.db).delete_datas(ids=ids.ids)
    return SuccessResponse("删除成功")
